﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
using ETWebApi2.ETWeiXin;

namespace ETWebApi2.Models
{
    public class SendMessage
    {
        #region 手机发短信
        /// <summary>
        /// 发送手机短信 
        /// </summary>
        /// <param name="phone">电话</param>
        /// <param name="cid">短信模板</param>
        public static int SendSM(string phone, string cid, string param)
        {

            /*   string mobile = "15271391194",
                   cid = "A44QVE6DR1Rj",
                   uid = "Mij4dkcMiTA3",
                   pas = "3tngu7n6", 
                   url = " http://api.weimi.cc/2/sms/send.html";
               byte[] byteArray = Encoding.UTF8.GetBytes("mob=" + mobile + "&cid=" + cid + "&p1=张继生&p2=W1123" + "&uid=" + uid + "&pas=" + pas + "&type=txt");*/
            byte[] byteArray = Encoding.UTF8.GetBytes("mob=" + phone + "&cid=" + cid + "&uid=Mij4dkcMiTA3&pas=3tngu7n6&type=txt" + param);

            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(new Uri("http://api.weimi.cc/2/sms/send.html"));

            webRequest.Method = "POST";
            webRequest.ContentType = "application/x-www-form-urlencoded";
            webRequest.ContentLength = byteArray.Length;

            Stream newStream = webRequest.GetRequestStream();
            newStream.Write(byteArray, 0, byteArray.Length);
            newStream.Close();

            //发送请求  
            HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();

            //得到返回结果
            StreamReader php = new StreamReader(response.GetResponseStream(), Encoding.Default);
            string Message = php.ReadToEnd();

            return int.Parse(Message);
        }
        #endregion

        #region 微信发送消息 
        public static void SendDebugText(HttpApplicationState app, string str)
        {
            WeiXinSendMessage wxs = new WeiXinSendMessage(app);
            string SME = wxs.GetAK("SME");
            wxs.Send(SME, str);
            wxs.SaveToken(app);
        }
        #endregion
    }
}